/**
 * @file tiny_def.h
 * @author 张恒宣
 * @LastEditTime 2025/4/07
 *
 * MIT License
 *
 * Copyright (c) 2025 张恒宣 luckkeymail@163.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __TINY_DEF_H__
#define __TINY_DEF_H__

/**********************
 *      类型定义
 **********************/

typedef unsigned char tiny_uint8_t;
typedef unsigned int tiny_uint32_t;
typedef unsigned long long tiny_uint64_t;
typedef char tiny_int8_t;
typedef int tiny_int32_t;
typedef long long tiny_int64_t;

#define COMPILE_TIME_ASSERT(cond, msg) typedef char static_assertion_##msg[(cond) ? 1 : -1]

/// @note tiny_uint64_t必须支持8字节
COMPILE_TIME_ASSERT(sizeof(tiny_uint64_t) == 8, tiny_uint64_t_size_must_be_8);

/// @brief 返回值类型
typedef enum
{
	/// @brief 失败
	tiny_false = 0,

	/// @brief 成功
	tiny_true
} tiny_ret_status_t;

/**********************
 *       宏定义
 **********************/
/// @brief 空指针
#define tiny_null (void *)(0)

/// @brief 获取结构体中成员的地址
#define STRUCT_TRANCING_PARENT(child, parent_type, member) \
	((parent_type *)((unsigned long)(child) - (unsigned long)(&((parent_type *)0)->member)))

/// @brief 滴答定时器分频
/// @note 需要在滴答定时器中除以该值, 表示1s执行多少个tick 100表示10ms一个tick 1000表示1ms一个tick
#define TINY_TICK_PER_SECOND   (100)
	
/// @brief 线程每个周期多少个tick 10表示线程消耗完10个tick发生切换
#define TINY_THREAD_INIT_TICK  (1)

/// @brief 将秒转换为tick
#define CONVERT_S_TO_TICK(s)   ((ms < 0) ? (-1) : ((s * TINY_TICK_PER_SECOND))

/// @brief 将毫秒转换为tick
#define CONVERT_MS_TO_TICK(ms) ((ms < 0) ? (-1) : (((ms + 9)  * TINY_TICK_PER_SECOND) / 1000))

/// @brief 忽略未使用变量的警告
#define UNUSED_VARIABLE(X)     ((void)(X))

/// @brief 忽略未使用函数参数的警告
#define UNUSED_PARAMETER(X)    UNUSED_VARIABLE(X)	

/// @brief 忽略无法返回的警告
#define NORETUTN              __attribute__((noreturn))

/**********************
 *   附加功能相关配置
 **********************/
/// @brief 打开配置
#define PROFILE_OPEN                   (1)

/// @brief 关闭配置
#define PROFILE_CLOSE                  (0)

/// @brief 是否开启初始化器
#define IS_OPEN_INITER                 PROFILE_CLOSE

/// @brief 是否启动CPU占用率统计
#define IS_OPEN_GET_CPU                PROFILE_CLOSE

/// @brief CPU占用率统计间隔ms
#define GET_CPU_TIME                   (1000)

/// @brief 是否启动栈水位检测
#define IS_OPEN_STACK_HIGH_WATER       PROFILE_CLOSE

/// @brief 水位栈填充
#define STACK_HIGH_WATER_FILL          (0xA5)

#endif //!__TINY_DEF_H__
